<?php
/**
 * Portfolio plugin intialization
 */

elgg_register_event_handler('init', 'system', 'portfolio_init');

/**
 * Initialize page handler and site menu item
 */
function portfolio_init() {
	elgg_register_library('elgg:oci_access', elgg_get_plugins_path() . 'blog/lib/oci_access.php');
	elgg_register_library('elgg:calendar', elgg_get_plugins_path() . 'portfolio/lib/calendar.php');
	elgg_register_library('elgg:portfolio', elgg_get_plugins_path() . 'portfolio/lib/portfolio.php');
	elgg_register_library('elgg:misc', elgg_get_plugins_path() . 'portfolio/lib/misc.php');	
	elgg_register_library('elgg:selected_pool', elgg_get_plugins_path() . 'portfolio/lib/selected_pool.php');	
	
	elgg_register_page_handler('portfolio', 'portfolio_page_handler');
	elgg_register_page_handler('lightbox', 'lightbox_page_handler');
	elgg_register_page_handler('trade', 'trade_page_handler');
	
	$action_path = elgg_get_plugins_path() . 'portfolio/actions';
	elgg_register_action('trade/comments', "$action_path/trade/save.php");

	$item = new ElggMenuItem('portfolio', elgg_echo('Portfolio'), 'portfolio');
	elgg_register_menu_item('site', $item);
	
	elgg_register_widget_type('holding', 'Top 5 Holdings', 'The "Top 5" widget');
	elgg_register_widget_type('trade', 'Latest Trades', 'The "Latest Trades" widget');
	elgg_register_widget_type('myfocus', 'Focused on', 'The "Focused on" widget');
	
	elgg_load_library('elgg:portfolio');
	elgg_load_library('elgg:calendar');
	elgg_load_library('elgg:misc');
}

/**
 * Portfolio page handler
 *
 * @param array $page url segments
 * @return bool
 */
function portfolio_page_handler($page) {
	$base = elgg_get_plugins_path() . 'portfolio/pages/portfolio';
	
	$vars = array();
	$vars['page'] = $page[0];
		
/*
	if (!isset($page[0])) {
		$page[0] = 'newest';
	}



	if ($page[0] == 'search') {
		$vars['search_type'] = $page[1];
		require_once "$base/search.php";
	} else {
		require_once "$base/index.php";
	}
*/
	
	require_once "$base/index.php";
	
	return true;
}

function lightbox_page_handler($page) {
	if ($page[0]=='comments') {
		$vars['page_type'] = 'lightbox';
		echo elgg_view('trade/comments', $vars);
	}
	return true;
}

function trade_page_handler($page) {
	elgg_load_js('lightbox');
	elgg_load_css('lightbox');
	elgg_load_js('jquery.form');
	elgg_load_js('elgg.embed');
	elgg_load_js('tinymce');
	elgg_load_js('elgg.tinymce');	

	$base = elgg_get_plugins_path() . 'portfolio/pages/trade';
	require_once "$base/comments.php";

	//echo elgg_view_page($title, elgg_view('trade/comments'));
	return true;
}




